package com.ymttest.database.sqlwapper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Select;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.mapping.couponbatchMapper;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.couponbatch;
import com.ymttest.utils.EnvSetup;
public class couponbatchWapper {
	 SqlManager manage;

	    public couponbatchWapper() {
	        manage = new SqlManager("mysqlcoupon" + EnvSetup.getEnv());
	    }

	   
	    public List<couponbatch> selectBybatchCode(String batchCode) {
	        List<couponbatch> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.selectByBatchCode",
	                        batchCode);
	        return result;
	    }
	    
	    public void setReceiveUserType(int ReceiveUserType,int BatchId) {
	    	 Map m = new HashMap();
	    	 m.put("ReceiveUserType", ReceiveUserType);
	    	 m.put("BatchId", BatchId); 
	         manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setReceiveUserType",
	                        m);
	         manage.commit();
	    }
	    public void setMaxUseTime(int ReceiveTimesPerUser,int BatchId) {
	    	 Map m = new HashMap();
	    	 m.put("ReceiveTimesPerUser", ReceiveTimesPerUser);
	    	 m.put("BatchId", BatchId); 
	         manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setMaxUseTime",
	                        m);
	         manage.commit();
	    }
	    
	    
	    
	    public List<Map> selectBatchInfoBysBatchCode(String sBatchCode) {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.selectBatchInfoBysBatchCode",
	                        sBatchCode);
	        return result;
	    }
	    public couponbatch selectByPrimaryKey(Integer batchId) {
	    	couponbatch result = (couponbatch) manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.selectByPrimaryKey",
	                        batchId).get(0);
	        return result;
	    }
	    public List<Map> selectTop5BatchInfoBysBatchCode() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.selectTop5BatchInfoBysBatchCode",
	                        null);
	        return result;
	    }
	 
	    public void setiReceiveCount(int BatchId, int ReceivedCount) {
	        Map m = new HashMap();
	        m.put("BatchId", BatchId);
	        m.put("ReceivedCount", ReceivedCount);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setiReceiveCount",
	                        m);
	        manage.commit();
	    }
	    public List<Map> getCouponBatchByBatchCode(String batchCode) {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getCouponBatchByBatchCode",
	                        batchCode);
	        return result;
	    }
	    public List<Map> getCouponBatchByOperatorId(Integer OperatorId) {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getCouponBatchByOperatorId",
	                        OperatorId);
	        return result;
	    }
	    public List<couponbatch> getCouponBatchByBagCode(String BagCode) {
	        List<couponbatch> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getCouponBatchByBagCode",
	                        BagCode);
	        return result;
	    }
	    public BigDecimal getCouponValueByValidEnd(String ValidEnd) {
	    	BigDecimal couponValue =new  BigDecimal(0);
	    	List<couponbatch> result =  manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getCouponValueByValidEnd",
	                        ValidEnd);
	        if(result.size()>0)
	        {
	        	couponValue=result.get(0).getCouponValue();
	        }
	        return couponValue;
	    }
	    
	    
	    
	    

	    
	    

	 

	    public List<Map> getTop1CouponBatch() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getTop1CouponBatch", null);
	        return result;
	    }

	    public List<Map> getTop1PlatformShortCodeCouponInfo() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getTop1PlatformShortCodeCouponBatch", null);
	        return result;
	    }

	    public List<Map> getTop1PlatformLinkCouponInfo() {
	        @SuppressWarnings("unchecked")
			List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getTop1PlatformLinkCouponBatch", null);
	        return result;
	    }
      /**
       * 获取商家全场优惠券
       * @return
       */
	    public List<Map> getTop1SellerCouponInfo() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getTop1SellerCouponBatch", null);
	        return result;
	    }

	    public List<Map> getTop1SellerProductCouponInfo() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getTop1SellerProductCouponBatch", null);
	        return result;
	    }

	    public List<Map> getAllSellerCouponList() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getAllSellerCouponList", null);
	        return result;
	    }

	    public List<Map> getPlatformShortCodeCouponInfo() {
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.getAllSellerCouponList", null);
	        return result;
	    }

	    public void setiCouponSettingId(int iBatchId, int iCouponSettingId) {
	        Map m = new HashMap();
	        m.put("iBatchId", iBatchId);
	        m.put("iCouponSettingId", iCouponSettingId);
	        manage.update(
	                "com.ymttest.database.mapping.couponbatchMapper.setiCouponSettingId",
	                m);
	        manage.commit();
	    }

	    public void setiBatchCreateType(String sbatchCode, int iBatchCreateType) {
	        Map m = new HashMap();
	        m.put("sbatchCode", sbatchCode);
	        m.put("iBatchCreateType", iBatchCreateType);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setiBatchCreateType",
	                        m);
	        manage.commit();
	    }

	    public void setbInvalidStatus(String sbatchCode, int bInvalidStatus) {
	        Map m = new HashMap();
	        m.put("sbatchCode", sbatchCode);
	        m.put("iBatchCreateType", bInvalidStatus);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setbInvalidStatus",
	                        m);
	        manage.commit();
	    }
	    
	    public void setiCouponTotalNum(String sbatchCode, int iCouponTotalNum) {
	        Map m = new HashMap();
	        m.put("sbatchCode", sbatchCode);
	        m.put("iCouponTotalNum", iCouponTotalNum);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setiCouponTotalNum",
	                        m);
	        manage.commit();
	    }

	    public void setIsShowInPage(String sbatchCode, Boolean IsShowInPage) {
	        Map m = new HashMap();
	        m.put("sbatchCode", sbatchCode);
	        m.put("IsShowInPage", IsShowInPage);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setIsShowInPage",
	                        m);
	        manage.commit();
	    }

	    public int getQueryValidCouponCount(int sellerId)
	    {
	    	List<Map> resultMap = manage
	                 .getSelectResults(
	                         "com.ymttest.database.mapping.couponbatchMapper.getQueryValidCouponCount", sellerId);
	    	int result=Integer.parseInt(resultMap.get(0).get("count").toString());
	         return result;
	    }
	    
	    public void setAcquireStartTime(String batchCode, Date acquireStartTime) {
	        Map m = new HashMap();
	        m.put("batchCode", batchCode);
	        m.put("acquireStartTime", acquireStartTime);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setAcquireStartTime",
	                        m);
	        manage.commit();
	    }
	    
	    public void setAcquireEndTime(String batchCode, Date acquireEndTime) {
	        Map m = new HashMap();
	        m.put("batchCode", batchCode);
	        m.put("acquireEndTime", acquireEndTime);
	        manage
	                .update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setAcquireEndTime",
	                        m);
	        manage.commit();
	    }
	    
	    public List<Map> selectSellerReceiveBatches(int sellerId,Date effectiveStartTime,Date effectiveEndTime) {
	    	 Map m = new HashMap();
	         m.put("sellerId", sellerId);
	         m.put("effectiveStartTime", effectiveStartTime);
	         m.put("effectiveEndTime", effectiveEndTime);
	         
	        List<Map> result = manage
	                .getSelectResults(
	                        "com.ymttest.database.mapping.couponbatchMapper.selectSellerReceiveBatches",
	                        m);
	        return result;
	    }
	    
		public List<Map> getSellerBatchesForApp(Integer pageSize, Integer sellerId, Integer batchStatus,
				Date lastBatchCreateTime) {
			Map m = new HashMap();
			m.put("pageSize", pageSize);
			m.put("sellerId", sellerId);
			m.put("batchStatus", batchStatus);
			m.put("lastBatchCreateTime", lastBatchCreateTime);

			List<Map> result = manage
					.getSelectResults("com.ymttest.database.mapping.couponbatchMapper.getSellerBatchesForApp", m);
			return result;
		}

	    public int getSellerBatchCountInCurrentMonth(int sellerId,Date startTime, Date endTime)
	    {
	    	Map m = new HashMap();
	        m.put("sellerId", sellerId);
	        m.put("startTime", startTime);
	        m.put("endTime", endTime);
	        
	    	int result = (int) manage
	                 .getSelectResults(
	                         "com.ymttest.database.mapping.couponbatchMapper.getSellerBatchCountInCurrentMonth", m).get(0);
	    	       return result;
	    }
	    
	    public List<Map> getUserWillExpireCouponCount(Integer pageSize, Integer days, Integer lastUserId)
	    {
	    	Map m = new HashMap();
	        m.put("pageSize", pageSize);
	        m.put("days", days);
	        m.put("lastUserId", lastUserId);
	        
	        List<Map> result =  manage
	                 .getSelectResults(
	                         "com.ymttest.database.mapping.couponbatchMapper.getUserWillExpireCouponCount", m);
	    	       
	        return result;
	    }
	    
	    public void setValidStart(int BatchId, Date ValidStart) {
	        Map m = new HashMap();
	        m.put("BatchId", BatchId);
	        m.put("ValidStart", ValidStart);
	        manage.update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setValidStart",
	                        m);
	        manage.commit();
	    }

	    public void setValidEnd(int BatchId, Date ValidEnd) {
	        Map m = new HashMap();
	        m.put("BatchId", BatchId);
	        m.put("ValidEnd", ValidEnd);
	        manage.update(
	                        "com.ymttest.database.mapping.couponbatchMapper.setValidEnd",
	                        m);
	        manage.commit();
	    }
	    
	    public static void main(String[] args) {
//	    	couponbatchWapper test = new couponbatchWapper();
//	    	Date date=new Date();
//	    	
//	        System.out.print(FormatUtil.beanToGSONString(test.getCouponValueByValidEnd(date)));
	    }
}
